﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class drug_AddPro : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAddpro_Click(object sender, EventArgs e)
    {
        string name = txtName.Text;
        string type = ddlCategory.SelectedValue;
        string price = txtPrice.Text;
        string description = txtDescription.Text;
        int stores = int.Parse(txtStore.Text);

        string fileName = FileUpload1.FileName;
        string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);

        if (fileType == "jpg" || fileType == "png" || fileType == "jpeg" || fileType == "bmp")
        {
            medical med = new medical();
            med.MedName = name;
            med.MedClass = type;
            med.MedPrice = price;
            med.Description = description;
            med.Store = stores;
            med.MedID = getID();
            med.Img = fileName;
            FileUpload1.SaveAs("E:\\mywebsite\\" + fileName);
            //FileUpload1.SaveAs("..\\imagine" + name);
            //med.im
            db.medical.InsertOnSubmit(med);
            db.SubmitChanges();
            Response.Write("<script>alert('添加药品成功!');</script>");
        }
        else
        {
            Response.Write("<script>alert('文件格式不符合要求!');</script>");

        }

        //Response.Write("<script>alert(" + fileType + ");</script>");
        
    }

    protected string getID()
    {
        var results = from r in db.medical
                      select r;
        int maxID = -1;
        if (results != null)
        {
            foreach (medical r in results)
            {
                int id = int.Parse(r.MedID);
                if (id > maxID)
                {
                    maxID = id;
                }
            }
        }
        return (maxID + 1).ToString();
    }
    

}